let noAdd = document.querySelector('[type="button"]')
noAdd.addEventListener('click',() => location.href = '/bookList')

let bookName = document.querySelector('[name=bookName]')
let author = document.querySelector('[name=author]')

document.querySelector('[type="submit"]').addEventListener('click',e => {
    e.preventDefault()
    let newBookName = bookName.value.trim()
    let newAuthor = author.value.trim()
    let isOK = true

    if(newBookName.trim().length === 0 && newAuthor.trim().length === 0){
        isOK = false
        alert('输入数据不能为空')
    }
    
    if (newAuthor.trim().length < 3 || newAuthor.trim().length > 8) {
        alert('作者的字符数量需要在 3 ~ 8 的范围内')
        isOK = false
    }

    if(!(/^《(.+?)》$/).test(newBookName)){
        alert('书本必须用 《》 符号包裹，并且内容不能为空')
        isOK = false
    }

    if (isOK) {
        document.querySelector('form').submit()
    }
    
})

